package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.Material;
import com.ruoyi.system.domain.Project;
import com.ruoyi.system.domain.Purchase;
import com.ruoyi.system.domain.PurchaseRecord;
import com.ruoyi.system.mapper.AnalysisMapper;
import com.ruoyi.system.service.AnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;

@Service
public class AnalysisServiceImpl implements AnalysisService {
    @Autowired
    private AnalysisMapper analysisMapper;

    @Override
    public List<PurchaseRecord> selectComparison(PurchaseRecord purchaseRecord) {
        return analysisMapper.selectComparison(purchaseRecord);
    }

    @Override
    public List<Purchase> selectInvoiceList(Integer choose) {
        if (choose == 0) {
            List<Purchase> purchases = analysisMapper.selectNoInvoiceList();
            return purchases;
        } else {
            List<Purchase> purchases = analysisMapper.selectInvoiceList();
            return purchases;
        }
    }

    //时间段查询
    @Override
    public List<PurchaseRecord> selectTimeList(String startTime, String endTime) {
        HashMap<String, String> map = new HashMap<>();
        DecimalFormat df = new DecimalFormat("######0.00");
        df.setRoundingMode(RoundingMode.HALF_UP);
        Double num = 0D;
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        List<PurchaseRecord> purchaseRecords = analysisMapper.selectTimeList(map);
        if (null != purchaseRecords && purchaseRecords.size() != 0) {
            for (int i = 0; i < purchaseRecords.size(); i++) {
                Double mdSum = purchaseRecords.get(i).getMdSum();
                Double mdPrice = purchaseRecords.get(i).getMdPrice();
                if (null == mdSum) {
                    mdSum = 0D;
                }
                if (null == mdPrice) {
                    mdPrice = 0D;
                }
                purchaseRecords.get(i).setSum(Double.valueOf(df.format(new BigDecimal(mdSum * mdPrice + ""))));
//                num+=purchaseRecords.get(i).getSum();

            }
            Double sum = analysisMapper.selectSum(map);
            purchaseRecords.get(0).setPickNum(Double.valueOf(df.format(new BigDecimal(sum + ""))));
        }

        return purchaseRecords;
    }

    @Override
    public List<PurchaseRecord> selectProject(Project project) {

        List<PurchaseRecord> purchaseRecords = analysisMapper.selectProject(project);

        return purchaseRecords;
    }

    @Override
    public List<PurchaseRecord> purchaseInfo(PurchaseRecord purchaseRecord) {
        List<PurchaseRecord> purchaseRecords = analysisMapper.analysisMapper(purchaseRecord);
        Double sum = 0.0;
      sum=analysisMapper.selectBussneissSum(purchaseRecord);
            purchaseRecords.get(0).setTruePrice(new BigDecimal(new Double(sum).toString()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());

        return purchaseRecords;
    }

    @Override
    public List<Purchase> selectSumPur(PurchaseRecord purchaseRecord) {
        return analysisMapper.selectSumPur(purchaseRecord);
    }
}
